home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-24  |  4.2 KB  |  200 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1987, 1988, 1989 Stanford University
  5.    Copyright (c) 1989, Tera Computer Company
  6.  **/
  7.  
  8.   /**
  9.      Stolen from the InterViews 2.5 bin source files.
  10.      Needed by another file
  11.    **/
  12.  
  13. #include "list2.h"
  14.  
  15. // BaseNode zeroes its pointers.
  16.  
  17. BaseNode::BaseNode () 
  18. {
  19.     prev = nil;
  20.     next = nil;
  21. }
  22.  
  23. BaseNode::~BaseNode () 
  24. {
  25.     // no storage allocated by base class
  26. }
  27.  
  28. // SameValueAs returns true if this class contains a data member of
  29. // any pointer type that has the same value as the given pointer.
  30.  
  31. boolean BaseNode::SameValueAs (void*) 
  32. {
  33.     // base class contains no data members
  34.     return false;
  35. }
  36.  
  37. // BaseList starts with only a header node.
  38.  
  39. BaseList::BaseList () 
  40. {
  41.     cur = head = new BaseNode;
  42.     head->prev = head->next = head;
  43.     size = 0;
  44. }
  45.  
  46. // ~BaseList destroys the list.
  47.  
  48. BaseList::~BaseList () 
  49. {
  50.     DeleteAll();
  51.     delete head;
  52. }
  53.  
  54. // Index returns the node that would be the indexed element if the
  55. // list was a C array (0 = first, 1 = next, ..., size-1 = last) or nil
  56. // if the index is out of bounds.  Index sets the current node to the
  57. // indexed node if it's in the list; otherwise, the current node
  58. // remains the same.
  59.  
  60. BaseNode* BaseList::Index (int index) 
  61. {
  62.     BaseNode* elem = nil;
  63.  
  64.     if (index >= 0 && index < size) 
  65.     {
  66.     elem = head->next;
  67.  
  68.     for (int i = 0; i < index; i++) 
  69.     {
  70.         elem = elem->next;
  71.     }
  72.  
  73.     cur = elem;
  74.     }
  75.  
  76.     return elem;
  77. }
  78.  
  79. // Find returns true if the list contains a node with a data member
  80. // whose value is the same as the given pointer or false if there is
  81. // no such node.  Find sets the current node to that node only if it
  82. // finds such a node; otherwise, the current node remains the same.
  83.  
  84. boolean BaseList::Find (void* value) 
  85. {
  86.     for (BaseNode* elem = head->next; elem != head; elem = elem->next) 
  87.     {
  88.     if (elem->SameValueAs(value)) 
  89.     {
  90.         cur = elem;
  91.         return true;
  92.     }
  93.     }
  94.  
  95.     return false;
  96. }
  97.  
  98. // Append inserts a node after the last node of the list.  The current
  99. // node remains the same.
  100.  
  101. void BaseList::Append (BaseNode* appendee) 
  102. {
  103.     BaseNode* last = head->prev;
  104.     appendee->prev = last;
  105.     appendee->next = head;
  106.     head->prev = appendee;
  107.     last->next = appendee;
  108.     ++size;
  109. }
  110.  
  111. // Prepend inserts a node before the first node of the list.  The
  112. // current node remains the same.
  113.  
  114. void BaseList::Prepend (BaseNode* prependee) 
  115. {
  116.     BaseNode* first = head->next;
  117.     prependee->prev = head;
  118.     prependee->next = first;
  119.     first->prev = prependee;
  120.     head->next  = prependee;
  121.     ++size;
  122. }
  123.  
  124. // InsertAfterCur inserts a node after the current node of the list.
  125. // The current node remains the same.
  126.  
  127. void BaseList::InsertAfterCur (BaseNode* prependee) 
  128. {
  129.     BaseNode* first = cur->next;
  130.     prependee->prev = cur;
  131.     prependee->next = first;
  132.     first->prev = prependee;
  133.     cur->next   = prependee;
  134.     ++size;
  135. }
  136.  
  137. // InsertBeforeCur inserts a node before the current node of the list.
  138. // The current node remains the same.
  139.  
  140. void BaseList::InsertBeforeCur (BaseNode* appendee) 
  141. {
  142.     BaseNode* last = cur->prev;
  143.     appendee->prev = last;
  144.     appendee->next = cur;
  145.     cur->prev  = appendee;
  146.     last->next = appendee;
  147.     ++size;
  148. }
  149.  
  150. // RemoveCur removes the current node of the list (if it's a node and
  151. // not the head) and sets the current node to the following node.
  152.  
  153. void BaseList::RemoveCur () 
  154. {
  155.     if (cur != head) 
  156.     {
  157.     BaseNode* before = cur->prev;
  158.     BaseNode* after  = cur->next;
  159.     after->prev  = before;
  160.     before->next = after;
  161.     cur = after;
  162.     --size;
  163.     }
  164. }
  165.  
  166. // DeleteCur deletes the current node of the list (if it's a node and
  167. // not the head) and sets the current node to the following node.
  168.  
  169. void BaseList::DeleteCur () 
  170. {
  171.     if (cur != head) 
  172.     {
  173.     BaseNode* before = cur->prev;
  174.     BaseNode* after  = cur->next;
  175.     after->prev  = before;
  176.     before->next = after;
  177.     delete cur;
  178.     cur = after;
  179.     --size;
  180.     }
  181. }
  182.  
  183. // DeleteAll deletes all the nodes of the list, making the list empty
  184. // again, and sets the current node to the list's head.
  185.  
  186. void BaseList::DeleteAll () 
  187. {
  188.     BaseNode* after = nil;
  189.  
  190.     for (BaseNode* doomed = head->next; doomed != head; doomed = after) 
  191.     {
  192.     after = doomed->next;
  193.     delete doomed;
  194.     }
  195.  
  196.     cur = head;
  197.     head->prev = head->next = head;
  198.     size = 0;
  199. }
  200.